package com.example.shengyin.frontController;
import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.*;
import com.example.shengyin.entity.Event;
import com.example.shengyin.service.EvenService;
import com.example.shengyin.service.JoinService;
import com.example.shengyin.service.UserService;
import com.example.shengyin.util.Md5Utils;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.SMSClient;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.*;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * AdminController
 *
 * @author ruoyi
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/front/join")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api(tags = "FrontJoinController", description = "活动预约")
public class FrontJoinController
{
    @Autowired
    private JoinService service;
    @Autowired
    private EvenService evenService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("活动预约，请先登录")
    @GetMapping("/add")
    public Result add(@ApiParam(name = "eventid",value = "活动id") @RequestParam(required = true)
                             Long eventid,HttpServletRequest request) {
        System.out.println(request.getHeader("Authorization"));
        User admin = (User) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin==null)
        {
            return Result.error("请先登录");
        }
        Event event = evenService.selectById(Math.toIntExact(eventid));
        if(event==null)
        {
            return Result.error("该活动不存在");
        }
        if(event.getFinishtime().before(new Date()))
        {
            return Result.error("该活动已过期");
        }
        Join join=new Join();
        join.setIsattend(false);
        join.setPhone(admin.getPhone());
        join.setEventid(eventid);
        join.setUname(admin.getUname());
        join.setUserid(admin.getId());
        join.setEventid(eventid);
        int insert = service.insert(join);
        if(insert==0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }
    @ApiOperation("查看自己的活动预约")
    @GetMapping("/look")
    public Result look(Page page, HttpServletRequest request) {
        System.out.println(request.getHeader("Authorization"));
        User admin = (User) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin==null)
        {
            return Result.error("请先登录");
        }
        int count = service.count(null,admin.getPhone(),null);
        List<Join> list = service.list(page, null,admin.getPhone(),null);
        if(count==0||list==null)
            return Result.success(page,"没有数据");
        page.setData(list);
        page.setCount(count);
        System.out.println(JSON.toJSONString(Result.success(page)));
        return Result.success(page);
    }
    @ApiOperation("删除自己的预约")
    @GetMapping("/delete/{id}")
    public Result delete(@ApiParam(name = "eventid",value = "活动id") @RequestParam(required = true)Long eventid,HttpServletRequest request)
    {
        System.out.println(request.getHeader("Authorization"));
        User admin = (User) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin==null)
        {
            return Result.error("请先登录");
        }
        Join select = service.select(eventid);
        if(select.getUserid()!=admin.getId())
        {
            return Result.error("您没有预约过该活动，无法删除");
        }
        int delete = service.delete(eventid);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }






}
